package miagk.elementy;

import java.io.InputStream;
import java.util.ArrayList;

import miagk.menu.R;

import com.threed.jpct.Animation;
import com.threed.jpct.Loader;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.util.BitmapHelper;

import android.content.res.Resources;

public class Wiatraczek implements Element{

	private Object3D model;
	private float x, y, z;
	private float skala;
	private String nazwa;
	private boolean naPlytce, zepsuty, dostepny;
	private float ind;
	
	public Wiatraczek(Resources res, float _x, float _y, float _z){
		Animation animation=new Animation(6);
		animation.createSubSequence("dziala");
		Object3D[] tmp;
		Object3D mergedTmp;
		zepsuty=false;
		dostepny=false;
		naPlytce=false;
		nazwa="Wiatraczek";
		ind=0.0f;
		x=_x;
		y=_y;
		z=_z;

		Object3D[] model2 = Loader.load3DS(res.openRawResource(R.raw.smig1_3ds), 0.01f);
		
		model = buildObject(model2);
		animation.addKeyFrame(model.getMesh());
		
		tmp=Loader.load3DS(res.openRawResource(R.raw.smig2_3ds), 0.01f);
		mergedTmp=buildObject(tmp);
		animation.addKeyFrame(mergedTmp.getMesh());

		tmp=Loader.load3DS(res.openRawResource(R.raw.smig3_3ds), 0.01f);
		mergedTmp=buildObject(tmp);
		animation.addKeyFrame(mergedTmp.getMesh());

		tmp=Loader.load3DS(res.openRawResource(R.raw.smig4_3ds), 0.01f);
		mergedTmp=buildObject(tmp);
		animation.addKeyFrame(mergedTmp.getMesh());
		
		tmp=Loader.load3DS(res.openRawResource(R.raw.smig5_3ds), 0.01f);
		mergedTmp=buildObject(tmp);
		animation.addKeyFrame(mergedTmp.getMesh());
		
		tmp=Loader.load3DS(res.openRawResource(R.raw.smig6_3ds), 0.01f);
		mergedTmp=buildObject(tmp);
		animation.addKeyFrame(mergedTmp.getMesh());
				
		model.setAnimationSequence(animation);
		model.strip();
		model.build();
		model.setRotationPivot(new SimpleVector(0,0,0));
		model.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
	}
	
	private Object3D buildObject(Object3D[] model2)
	{
		Object3D tmp;
		model2[0].calcTextureWrapSpherical();
		model2[0].setTexture("texture2");
		model2[0].setCenter(SimpleVector.ORIGIN);
		
		model2[1].calcTextureWrapSpherical();
		model2[1].setTexture("texture4");
		model2[1].setCenter(SimpleVector.ORIGIN);
		
		model2[2].calcTextureWrapSpherical();
		model2[2].setTexture("texture4");
		model2[2].setCenter(SimpleVector.ORIGIN);
		
		model2[3].calcTextureWrapSpherical();
		model2[3].setTexture("texture8");
		model2[3].setCenter(SimpleVector.ORIGIN);
		
		tmp = Object3D.mergeAll(model2);
		tmp.calcBoundingBox();
		tmp.calcNormals();
		tmp.rotateX((float)Math.PI);
		return tmp;
	}
	
	public void refreshModel(){
		//model.position().x=recalculatePosition(x);
	}
	
	private float recalculatePosition(int a){
		return 1.0f;
	}
	
	public Object3D getModel() {
		return model;
	}

	public float getScale(){
		return skala;
	}
	
	public void setScale(float s){
		skala=s;
	}
	
	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}

	public float getZ() {
		return z;
	}

	public String getName() {
		return nazwa;
	}

	public void setModel(Object3D m) {
		this.model=m;
	}

	public void setX(float _x) {
		x=_x;
	}

	public void setY(float _y) {
		y=_y;
	}

	public void setZ(float _z) {
		z=_z;
	}

	public void setName(String name) {
		nazwa=name;
	}

	public void setCzyZepsuty(boolean czyZ) {
		zepsuty=czyZ;
	}

	public void setCzyWUkladzie(boolean czyNP) {
		naPlytce=czyNP;
	}



	public boolean czyZepsuty() {
		return zepsuty;
	}

	public boolean CzyNaPlytce() {
		return naPlytce;
	}

	public void animuj() {
		if(!naPlytce) return;
		{
            ind += 0.018f;
            if (ind > 1f) {
                ind -= 1f;
            }
        }
        model.animate(ind, 1);
	}


}
